package com.link.base.base.autoconfig.nocode.controller;

import com.link.base.base.autoconfig.nocode.model.LnkInnerCode;
import com.link.base.base.autoconfig.nocode.model.SourceCodeObject;
import com.link.base.base.autoconfig.nocode.service.LnkInnerCodeService;
import com.link.base.base.autoconfig.nocode.service.PackageNameProcessor;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自动配置的代码版本
 *
 * @author 何启谦
 * @date 2019/09/06 14:36
 */
@Controller
@RequestMapping("/link/lnkInnerCode")
public class LnkInnerCodeController extends BasicController<LnkInnerCode> {

    @Resource
    private LnkInnerCodeService lnkInnerCodeService;

    @Override
    public BasicService<LnkInnerCode> getBasicService() throws Exception {
        return lnkInnerCodeService;
    }

    /**
     * 生成最新代码，只是实时生成，并不会插入到数据库
     */
    @RequestMapping({"/generateCode"})
    @ResponseBody
    public Map<String, Object> generateCode(@RequestBody LnkInnerCode entity) {
        Map<String, Object> result = new HashMap<>();
        Long headId = entity.getHeadId();
        try {
            LnkInnerCode lnkInnerCode = lnkInnerCodeService.generateCode(headId);
            result.put("lnkInnerCode", lnkInnerCode);
            result.put("success", true);
            result.put("code", "200");
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("code", "400");
        }
        return result;
    }

    /**
     * 根据headId查询当前部署的行
     */
    @RequestMapping({"/queryDeployedByHeadId"})
    @ResponseBody
    public Map<String, Object> queryDeployedByHeadId(@RequestBody LnkInnerCode entity) {
        Map<String, Object> result = new HashMap<>();
        //对象路径
        Long headId = entity.getHeadId();
        try {
            LnkInnerCode lnkInnerCode = lnkInnerCodeService.queryDeployedByHeadId(headId);
            result.put("lnkInnerCode", lnkInnerCode);
            result.put("success", true);
            result.put("code", "200");
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("code", "400");
        }
        return result;
    }

    /**
     * 根据modelPath查询当前部署的Vue代码
     */
    @RequestMapping({"/getDeployedVueCode"})
    @ResponseBody
    public Map<String, Object> getDeployedVueCode(@RequestBody LnkInnerCode entity) {
        Map<String, Object> result = new HashMap<>();
        //对象路径
        String modelPath = entity.getModelPath();
        try {
            String vueCode = lnkInnerCodeService.getDeployedVueCode(modelPath);
            result.put("vueCode", vueCode);
            result.put("success", true);
            result.put("code", "200");
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("code", "400");
        }
        return result;
    }

    /**
     * 校验代码
     */
    @RequestMapping({"/checkCode"})
    @ResponseBody
    public Map<String, Object> checkCode(@RequestBody LnkInnerCode entity) {
        Map<String, Object> result = new HashMap<>();
        Long id = entity.getId();
        try {
            lnkInnerCodeService.checkCode(id);
            result.put("success", true);
            result.put("code", "200");
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("code", "400");
        }
        return result;
    }

    /**
     * 发布版本
     */
    @RequestMapping({"/deploy"})
    @ResponseBody
    public Map<String, Object> deploy(@RequestBody LnkInnerCode entity) {
        Map<String, Object> result = new HashMap<>();
        Long id = entity.getId();
        try {
            lnkInnerCodeService.deploy(id);
            result.put("success", true);
            result.put("code", "200");
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("code", "400");
        }
        return result;
    }

    /**
     * 卸载
     */
    @RequestMapping({"/uninstall"})
    @ResponseBody
    public Map<String, Object> uninstall(@RequestBody LnkInnerCode entity) {
        Map<String, Object> result = new HashMap<>();
        Long headId = entity.getHeadId();
        try {
            if (headId == null) {
                throw new IllegalArgumentException("headId 不允许为空");
            }
            lnkInnerCodeService.uninstall(headId);
            result.put("success", true);
            result.put("code", "200");
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("code", "400");
        }
        return result;
    }

    /**
     * 新建，然后发布版本
     */
    @RequestMapping({"/insertAndDeploy"})
    @ResponseBody
    public Map<String, Object> insertAndDeploy(@RequestBody LnkInnerCode entity) {
        Map<String, Object> result = new HashMap<>();
        try {
            lnkInnerCodeService.insertAndDeploy(entity);
            LnkInnerCode newRow = lnkInnerCodeService.queryById(entity.getId());
            result.put("newRow", newRow);
            result.put("success", true);
            result.put("code", "200");
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("code", "400");
        }
        return result;
    }

    /**
     * 重新部署所有计划要部署的版本
     */
    @RequestMapping({"/getAllPlanDeployCodeObject"})
    @ResponseBody
    public Map<String, Object> getAllPlanDeployCodeObject() {
        Map<String, Object> result = new HashMap<>();
        try {
            HashMap<Long, String> failMessages = new HashMap<>();

            // 所有计划部署的
            List<LnkInnerCode> planDeploys = lnkInnerCodeService.queryAllPlanDeploy();

            List<SourceCodeObject> sourceCodeObjects = new ArrayList<>();
            // 遍历开始部署
            for (LnkInnerCode item : planDeploys) {
                try {
                    SourceCodeObject sourceCodeObject = PackageNameProcessor.createSourceCodeObject(item);
                    sourceCodeObjects.add(sourceCodeObject);
                } catch (Exception e) {
                    failMessages.put(item.getId(), e.getMessage());
                    LCLogger.withException(e);
                }
            }
            result.put("failMessages", failMessages);
            result.put("rows", sourceCodeObjects);
            result.put("success", true);
            result.put("code", "200");
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("code", "400");
        }
        return result;
    }
}
